#include <csignal>
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <sstream>

static struct sigaction siga;

void handler(int sig, siginfo_t *siginfo, void *context) {
	std::stringstream ss;
	ss << "echo \"" << "SIG: " << sig << ", pid: " << siginfo->si_pid << ", uid: " << siginfo->si_uid;
	ss << ", runlevel: `runlevel`, top: `strings /run/utmp | head -1`, service: `systemctl list-jobs shutdown.target`\" >> /opt/signal_test";
	std::string s = ss.str();
	printf("%s\n", s.c_str());
	system(s.c_str());
	if(sig == 15) {
		exit(sig);
	}
	
}

int main(int argc, const char ** argv) {
	siga.sa_sigaction = handler;
	siga.sa_flags |= SA_SIGINFO;
	sigaction(1, &siga, nullptr);
	sigaction(2, &siga, nullptr);
	sigaction(3, &siga, nullptr);
	sigaction(4, &siga, nullptr);
	sigaction(5, &siga, nullptr);
	sigaction(6, &siga, nullptr);
	sigaction(7, &siga, nullptr);
	sigaction(8, &siga, nullptr);
	sigaction(9, &siga, nullptr);
	sigaction(10, &siga, nullptr);
	sigaction(11, &siga, nullptr);
	sigaction(12, &siga, nullptr);
	sigaction(13, &siga, nullptr);
	sigaction(14, &siga, nullptr);
	sigaction(15, &siga, nullptr);
	// sigaction(17, &siga, nullptr);
	// sigaction(18, &siga, nullptr);
	while(1) {}
	return 0;
}